<%@page import="com.funtl.framework.web.map.BaseInfo"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<title><%=BaseInfo.PROJECT_NAME%> - 权限菜单管理</title>
<jsp:include page="/head.jsp" />
<script type="text/javascript">
	function add() {
		$("#dlg_manager").dialog("open");
		$("#fm_manager").form("clear");
	}
	
	function edit() {
    	var row = $("#dg_list").datagrid("getSelected");
    	if (row == null) {
    		dialog.info("请选择一条记录");
    	} else {
    		$("#dlg_manager").dialog("open");
    		$("#purview_menu_id").val(row.id);
    		$("#purview_menu_name").val(row.menuName);
    		$("#purview_menu_url").val(row.menuUrl);
    		$("#purview_menu_no").val(row.menuNo);
    		$("#purview_column_id").combobox("setValue", row.purviewColumnId);
    	}
    }
    
    function del() {
    	var row = $("#dg_list").datagrid("getSelected");
    	if (row == null) {
    		dialog.info("请选择一条记录");
    	} else {
    		dialog.confirm("确定要删除记录吗？", function() {
    			var data = {
    				"purviewMenu.id" : row.id
    			};
    			ajax.post("system/purview/menu/ajax/delete", data, function(data) {
    				if (data.message == null || data.message.length == 0) {
    					$("#dg_list").datagrid("reload");
    					dialog.info("删除成功");
    				} else {
    					dialog.info(data.message);
    				}
    			});
    		});
    	}
    }
	
	var dlgManagerBtn = [{
        text:"保存",
        iconCls:"icon-ok",
        handler:function() {
        	if ($("#purview_menu_id").val() == "") {
        		$("#fm_manager").attr("action", "system/purview/menu/ajax/insert");
        	} else {
        		$("#fm_manager").attr("action", "system/purview/menu/ajax/update");
        	}
        	
        	form.submit("fm_manager", function(data) {
				if (data.message == null || data.message.length == 0) {
					$("#fm_manager").form("clear");
					$("#dlg_manager").dialog("close");
					$("#dg_list").datagrid("reload");
					dialog.info("保存成功");
				} else {
					dialog.info(data.message);
				}
			});
        }
    },{
        text:"取消",
        iconCls:"icon-cancel",
        handler:function() {
        	$("#dlg_manager").dialog("close");
        }
    }];
	
	//合并相同栏目名称单元格
	function onLoadSuccess(data) {
		var merges = new Array();
		var mergeColumnIndex = 0;
		var mergeColumnCount = 0;
		
		for (var i = 0 ; i < data.rows.length ; i++) {
			if (i < data.rows.length - 1) {
				if (data.rows[i].params != data.rows[i + 1].params) {
					var merge = {index: mergeColumnIndex, rowspan: mergeColumnCount + 1};
					merges.push(merge);
					
					mergeColumnIndex = i + 1;
					mergeColumnCount = 0;
				} else {
					mergeColumnCount++;
				}
			} else {
				var merge = {index: mergeColumnIndex, rowspan: mergeColumnCount + 1};
				merges.push(merge);
			}
		}
		
		for ( var i = 0; i < merges.length; i++) {
			$(this).datagrid('mergeCells', {
				index : merges[i].index,
				field : "params",
				rowspan : merges[i].rowspan
			});
		}
	}
</script>
</head>

<body>
	<table id="dg_list" class="easyui-datagrid" data-options="rownumbers:true,singleSelect:true,url:'system/purview/menu/ajax/query',onLoadSuccess:onLoadSuccess,toolbar:'#dg_list_toolbar'">
  		<thead>
  			<tr>
  				<th data-options="field:'params'">栏目名称</th>
  				<th data-options="field:'menuName'">菜单名称</th>
  				<th data-options="field:'menuUrl'">链接地址</th>
  				<th data-options="field:'menuNo'">排序序号</th>
  				<th data-options="field:'createName'">创建人</th>
  				<th data-options="field:'createDate', formatter:formatter.formatDateTime">创建日期</th>
  				<th data-options="field:'updateName'">修改人</th>
  				<th data-options="field:'updateDate', formatter:formatter.formatDateTime">修改日期</th>
  			</tr>
  		</thead>
  	</table>
  	<div id="dg_list_toolbar" style="padding:5px;height:auto">
   		<div style="margin-bottom:5px">
	  		<a class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add();">新增</a> 
			<a class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="edit();">编辑</a> 
			<a class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="del();">删除</a>
		</div>
   	</div>
   	
   	<div id="dlg_manager" class="easyui-dialog" style="width:400px;height:auto;padding:10px" data-options="title:'信息管理',buttons:dlgManagerBtn,modal:true,closed:true">
   		<form id="fm_manager" method="post" action="">
   			<input id="purview_menu_id" type="hidden" name="purviewMenu.id" />
   			<table align="center">
   				<tr>
	    			<td align="right">菜单名称：</td>
	    			<td><input id="purview_menu_name" class="easyui-validatebox" type="text" name="purviewMenu.menuName" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td align="right">链接地址：</td>
	    			<td><input id="purview_menu_url" class="easyui-validatebox" type="text" name="purviewMenu.menuUrl" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td align="right">排序序号：</td>
	    			<td><input id="purview_menu_no" class="easyui-validatebox" type="text" name="purviewMenu.menuNo" data-options="required:true"></input></td>
	    		</tr>
	    		<tr>
	    			<td align="right">分栏名称：</td>
	    			<td>
	    				<select class="easyui-combobox" id="purview_column_id" name="purviewMenu.purviewColumnId" data-options="required:true">
	    					<s:iterator value="#request.purviewColumns" var="v" status="state">
	    						<option value="<s:property value='#v.id' />"><s:property value='#v.columnName' /></option>
	    					</s:iterator>
	    				</select>
	    			</td>
	    		</tr>
   			</table>
   		</form>
   	</div>
</body>
</html>